<?php

class CreateOrUpdatePost extends Zend_Form {
	
	private $advertisement;
	private $password;
	private $fromCountry;
	private $toCountry;
	
	public function CreateOrUpdatePost($advertisement, $password, $fromCountry=null, $toCountry=null) {
		$this->advertisement = $advertisement;
		$this->password = $password;
		$this->fromCountry = $fromCountry;
		$this->toCountry = $toCountry;
		$this->__construct();
	}
	
	public function init() {
		$config = Zend_Registry::get("config");
		if (isset($this->advertisement)) {
			$user = Advertisement::getUser($this->advertisement->id);
			$element = new Zend_Form_Element_Hidden(Advertisement::ID);
			$element->setValue($this->advertisement->id);
			$this->addElement($element);
			
			$element = new Zend_Form_Element_Hidden(Advertisement::PASSWORD);
			$element->setValue($this->password);
			$this->addElement($element);
			$this->setAction($config->baseurl.'/bulletin/update');
		} else {
			$this->setAction($config->baseurl.'/bulletin/create');
		}
		
//		$element = new Zend_Form_Element_Hidden('city');
//		$element->setValue($this->city->id);
//		$this->addElement($element);
			
		$this->setMethod('POST');
		$this->setName('createAdForm');
		$element = new Zend_Form_Element_Text(User::NAME);
		$element->setLabel('怎么称呼您');
		$element->setDescription('必填，中英文都可，2到30个字');
		$element->setRequired(true);		
		//$element->addValidator(new Zend_Validate_StringLength(2,30));
		if (isset($user)) {
			$element->setValue($user->name);
		}
		$this->addElement($element);
		
		$element = new Zend_Form_Element_Text(User::EMAIL);
		$element->setLabel('您的Email');
		$element->setRequired(true);
		$element->addValidator(new Zend_Validate_EmailAddress());	
		$element->addValidator('NotEmpty');
		$element->setDescription('必填，将不会显示在您的帖子里');
		if (isset($user)) {
			$element->setValue($user->email);
		}
		$this->addElement($element);
		
		$element = new Zend_Form_Element_Text(User::MOBILE);
		$element->setLabel('您的手机号');		
		$element->addFilter(new Zend_Filter_HtmlEntities());
		$element->addFilter(new Zend_Filter_StripTags());
		$element->setDescription('可选，方便其他用户联系您');
		if (isset($user)) {
			$element->setValue($user->mobile);
		}
		$this->addElement($element);
				
		
		$this->addDisplayGroup(array('name', 'email','mobile'),'aboutYou');
		$this->getDisplayGroup('aboutYou')->removeDecorator('DtDdWrapper');
		
		
		// radio box
		$element = new Zend_Form_Element_Radio(Advertisement::TYPE);
		$element->addMultiOptions(
			array(
				'lease' => '供',
				'want' => '求'
			))->removeDecorator('Label');
		$element->setSeparator('')->setValue(Advertisement::LEASE);
		if (isset($this->advertisement)) {
			$element->setValue($this->advertisement->type);
		}
		$this->addElement($element);
		
		$element = new Zend_Form_Element_Select("from_country");
        $element->setLabel("出发国家");
        $element->addMultiOption(0, "-出发国家-");
        $element->addMultiOptions(Country::getAvailableCountriesAsArray());
        if (!empty($this->fromCountry)) {
        	$element->setValue($this->fromCountry);
        }
        $this->addElement($element);
        $element = new Zend_Form_Element_Select(Advertisement::FROM_CITY);
        $element->setLabel("出发城市");
        $element->addMultiOption(0, "-出发城市-");
        if (!empty($this->fromCountry)) {
	        $element->addMultiOptions(Country::getCityByCountryAsArray($this->fromCountry));
	        if (!empty($this->advertisement->from_city)) {
	        	$element->setValue($this->advertisement->from_city);
	        }
        }
        $this->addElement($element);
		
		$element = new Zend_Form_Element_Text(Advertisement::DEPARTURE_TIME);
		$element->setLabel("出发时间");
		if (isset($this->advertisement)) {
			$element->setValue($this->advertisement->departure_time);
		}
		$element->addValidator('NotEmpty');
		$element->setDescription('必填');
		$this->addElement($element);
		
		$element = new Zend_Form_Element_Select("to_country");
        $element->setLabel("抵达国家");
        $element->addMultiOption(0, "-抵达国家-");
        $element->addMultiOptions(Country::getAvailableCountriesAsArray());
        if (!empty($this->toCountry)) {
        	$element->setValue($this->toCountry);
        }
        $this->addElement($element);
		
		$element = new Zend_Form_Element_Select(Advertisement::TO_CITY);
        $element->setLabel("抵达城市");
        $element->addMultiOption(0, "-抵达城市-");
        if (!empty($this->toCountry)) {
	        $element->addMultiOptions(Country::getCityByCountryAsArray($this->toCountry));
	        if (!empty($this->advertisement->to_city)) {
	        	$element->setValue($this->advertisement->to_city);
	        }
        }
        $this->addElement($element);
		
		$element = new Zend_Form_Element_Text(Advertisement::ARRIVAL_TIME);
		$element->setLabel("抵达时间");
		if (isset($this->advertisement)) {
			$element->setValue($this->advertisement->arrival_time);
		}
		$element->addValidator('NotEmpty');
		$element->setDescription('必填');
		$this->addElement($element);
		
		$element = new Zend_Form_Element_Text(Advertisement::WEIGHT);
		$element->setLabel('可带行李重量(单位:公斤)');
		if (isset($this->advertisement)) {
			$element->setValue($this->advertisement->weight);
		}
		$element->addValidator('NotEmpty');
		$element->setDescription('必填');
		$this->addElement($element);
		
		$element = new Zend_Form_Element_Textarea(Advertisement::DESCRIPTION);
		$element->setLabel('描述');
		$element->setDescription('具体描述，例如: 小物件50kr起带，超过1公斤的，每超过一公斤加100kr，主要是想交个朋友，下次也帮我带哈~~也可以帮邮寄');
		$element->setAttrib('rows',4);
		if (isset($this->advertisement)) {
			$element->setValue($this->advertisement->description);
		}
		$this->addElement($element);
		
		$element = new Elements();
		$element->addReCaptcha($this);
		
		$element = new Zend_Form_Element_Submit('post');
		$element->setValue('提交')->removeDecorator('Label');
		$this->addElement($element);
		
		$this->addDisplayGroup(array('type','from_country','from_city','departure_time','to_country','to_city','arrival_time','weight','description','challenge','post'),'aboutLuggage');
		$this->getDisplayGroup('aboutLuggage')->removeDecorator('DtDdWrapper');
	}
}